#include "window.h"
//#include "common.h"

/* 功能实现 */
void show_info(gpointer window);
void show_error(gpointer window);
void show_question(gpointer window);
void show_warning(gpointer window);

/*槽*/
inline void on_infoPushButton_clicked(GtkWidget *widget, gpointer window);
inline void on_errorPushButton_clicked(GtkWidget *widget, gpointer window);
inline void on_questionPushButton_clicked(GtkWidget *widget, gpointer window);
inline void on_warningPushButton_clicked(GtkWidget *widget, gpointer window);

/* test */
void message_dialog_test(GtkWidget *window, GtkWidget *vbox);

MainWindow *window_new()
{
    MainWindow *w;
    GtkWidget *window;
    GtkWidget *vbox;

    MenuBar *menubar;
    ToolBar *toolbar;

    GtkWidget *label;

    w = (MainWindow *)g_malloc(sizeof(MainWindow));

    /* window */
    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
    gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
    gtk_window_set_resizable(GTK_WINDOW(window), FALSE);        /*设置窗口不能拉伸*/
    gtk_window_set_title(GTK_WINDOW(window), "dialog_demo");
    gtk_widget_set_size_request (GTK_WIDGET(window), 300, 250); /*设置窗口大小*/
//    gtk_container_set_border_width(GTK_CONTAINER(window), 15);/*设置容器边界大小*/
    w->window = window;

    /* vbox */
    vbox = gtk_vbox_new(FALSE, 0);
    gtk_container_add(GTK_CONTAINER(window), vbox);

    /* menubar */
    menubar = menubar_new(w);
    gtk_box_pack_start(GTK_BOX(vbox), menubar->menubar, FALSE, FALSE, 3);
    w->menubar = menubar;

    /* toolbar */
    toolbar = toolbar_new(w);
    gtk_box_pack_start(GTK_BOX(vbox), toolbar->toolbar, FALSE, FALSE, 3);
    w->toolbar = toolbar;

    /* label */
    label = gtk_label_new("Zet Code");
    gtk_label_set_justify(GTK_LABEL(label), GTK_JUSTIFY_CENTER);
    gtk_box_pack_start(GTK_BOX(vbox), label, FALSE, FALSE, 5);
    w->label = label;

    /* test */
    message_dialog_test(window, vbox);

    /* signal setting */
    g_signal_connect(G_OBJECT(window), "destroy", G_CALLBACK(gtk_main_quit), NULL);
    gtk_widget_show_all(window);

    return w;
}

void message_dialog_test(GtkWidget *window, GtkWidget *vbox)
{
    GtkWidget *table;
    GtkWidget *infoPushButton, *errorPushButton, *questionPushButton, *warningPushButton;

    table = gtk_table_new(2, 2, TRUE);
    gtk_table_set_row_spacings(GTK_TABLE(table), 2);
    gtk_table_set_col_spacings(GTK_TABLE(table), 2);
    gtk_box_pack_start(GTK_BOX(vbox), table, FALSE, FALSE, 3);
//    gtk_container_add(GTK_CONTAINER(window), table);

    infoPushButton = gtk_button_new_with_label("Info");
    errorPushButton = gtk_button_new_with_label("Error");
    warningPushButton = gtk_button_new_with_label("Warning");
    questionPushButton = gtk_button_new_with_label("Question");

    gtk_table_attach(GTK_TABLE(table), infoPushButton, 0, 1, 0, 1, GTK_FILL, GTK_FILL, 3, 3);
    gtk_table_attach(GTK_TABLE(table), warningPushButton, 1, 2, 0, 1, GTK_FILL, GTK_FILL, 3, 3);
    gtk_table_attach(GTK_TABLE(table), questionPushButton, 0, 1, 1, 2, GTK_FILL, GTK_FILL, 3, 3);
    gtk_table_attach(GTK_TABLE(table), errorPushButton, 1, 2, 1, 2, GTK_FILL, GTK_FILL, 3, 3);

    g_signal_connect(G_OBJECT(infoPushButton), "clicked", G_CALLBACK(on_infoPushButton_clicked), (gpointer)window);
    g_signal_connect(G_OBJECT(questionPushButton), "clicked", G_CALLBACK(on_questionPushButton_clicked), (gpointer)window);
    g_signal_connect(G_OBJECT(errorPushButton), "clicked", G_CALLBACK(on_errorPushButton_clicked), (gpointer)window);
    g_signal_connect(G_OBJECT(warningPushButton), "clicked", G_CALLBACK(on_warningPushButton_clicked), (gpointer)window);
}



inline void on_infoPushButton_clicked(GtkWidget *widget, gpointer window)
{
    show_info(window);
}

inline void on_errorPushButton_clicked(GtkWidget *widget, gpointer window)
{
    show_error(window);
}

inline void on_questionPushButton_clicked(GtkWidget *widget, gpointer window)
{
    show_question(window);
}

inline void on_warningPushButton_clicked(GtkWidget *widget, gpointer window)
{
    show_warning(window);
}

void show_info(gpointer window)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(GTK_WINDOW(window),
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_INFO,
                                    GTK_BUTTONS_OK,
                                    "Download Completed");
    gtk_window_set_title(GTK_WINDOW(dialog), "Information");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

void show_error(gpointer window)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(GTK_WINDOW(window),
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_ERROR,
                                    GTK_BUTTONS_OK,
                                    "Error loading file");
    gtk_window_set_title(GTK_WINDOW(dialog), "Error");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

void show_question(gpointer window)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(GTK_WINDOW(window),
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_QUESTION,
                                    GTK_BUTTONS_YES_NO,
                                    "Are you sure to quit?");
    gtk_window_set_title(GTK_WINDOW(dialog), "Question");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}

void show_warning(gpointer window)
{
    GtkWidget *dialog;
    dialog = gtk_message_dialog_new(GTK_WINDOW(window),
                                    GTK_DIALOG_DESTROY_WITH_PARENT,
                                    GTK_MESSAGE_WARNING,
                                    GTK_BUTTONS_OK,
                                    "Unallowed operation");
    gtk_window_set_title(GTK_WINDOW(dialog), "Warning");
    gtk_dialog_run(GTK_DIALOG(dialog));
    gtk_widget_destroy(dialog);
}
